home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7252 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  79 lines

  1. Path: pegasus.montclair.edu!harmon
  2. From: harmon@pegasus.montclair.edu (Derek Harmon)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Limit on #bytes inside of struct?
  5. Date: 12 Feb 1996 15:31:18 -0500
  6. Organization: Montclair State University
  7. Message-ID: <harmon.824157042@pegasus.montclair.edu>
  8. References: <4feg1d$d4g@cville-srv.wam.umd.edu> <4ffg6b$ivd@sparcserver.lrz-muenchen.de> <4ffohq$1gb@mordred.gatech.edu> <4ffun7$1l4l@cymbal.aix.calpoly.edu> <311F15D8.78D1@zess.uni-siegen.de>
  9. NNTP-Posting-Host: pegasus.montclair.edu
  10. X-Newsreader: NN version 6.5.0 #68 (NOV)
  11.  
  12. ** Quoting a message by <becker@zess.uni-siegen.de> dated <12-Feb-1996>:
  13. > No it isn't, see below.
  14.  
  15.    Yes it is, see below below.  :)
  16.  
  17. > >    typedef struct {
  18. > >       int the_array[50000];
  19. > >    } struct_type;
  20. >
  21. > This way, you don't put the 50K numbers in the struct, but only
  22. > a pointer to them. Should be at most 4 bytes.
  23.  
  24.    First of all, we should agree that there are 50,000 continiguous integers
  25. *SOMEPLACE*.  We can rule out having malloc'd them, so they aren't sitting
  26. on the heap, as they haven't been dynamically allocated.  In fact, this is
  27. a static allocation of an array with a fixed number of elements, determined
  28. at compile-time.
  29.  
  30.    I believe you've confused the degeneration of an array name, when used
  31. without square brackets, into a pointer during execution; and the declaration
  32. of arrays and pointers.
  33.  
  34. int *p;   /* This is a pointer to an integer, a pointer's declaration will
  35.            * always feature a asterisk (*). */
  36.  
  37. int n[10];  /* This is an array declaration, it will always feature square
  38.              * brackets ([]). */
  39. int main(void)
  40. {
  41.  ...
  42.  p++;  /* This is a POINTER, whose address is incremented by sizeof(int) */
  43.  n[1] = *p + 2;  /* This is the VALUE at p being added to 2 and placed
  44.                   * into the second spot of array n. */
  45.  n[1] = n[0] + 2; /* This is the VALUE of n's first spot, plus 2, and
  46.                    * placed into the second spot of array n. */
  47.  p = n; /* This sets p, a POINTER, to n, here having degenerated from an
  48.          * ARRAY into a POINTER. */
  49.  ...
  50. }
  51.  
  52.    So some observations become clear.  In your data declarations, pointers
  53. always have *'s, arrays always have []'s.  In your program, pointers that
  54. are being "dereferenced," or producing a value to which they point, will
  55. always have *'s, and arrays producing a value of which they hold, will
  56. always have []'s.  Only within your function declarations when an array
  57. name is w/o []'s and a pointer is w/o *'s are they rougly equivalent (and
  58. then, they have nothing to do with ints, chars, or other single variables
  59. which NEVER have *'s or []'s associated with them).
  60.  
  61. > >       int the_array[50000];
  62.                        ^     ^
  63.                        !     !
  64.    Therefore, these square brackets betray this as an Array Declaration,
  65. not a Pointer Declaration, which would have looked like this,
  66.  
  67. :         int *the_array;
  68.               ^
  69.               !
  70.   (This shouts POINTER!)
  71.  
  72.                                                      -- Stone
  73. --
  74. # Derek Harmon (aka Stonelight)    harmon@pegasus.montclair.edu
  75. # - Computer Science Undergrad, Montclair State University, NJ
  76. # - My views are my own, nobody else is this creative.  3;)>
  77. ... Santa's elves are just a bunch of subordinate Clauses.
  78.  
  79.